$(function () {
  axios.get("http://localhost:9000/goods/category").then(function (res) {
    if (res.data.code === 1) {
      let html = '<li class="active">全部</li>';
      res.data.list.forEach(function (item, index) {
        html += `<li>${item}</li>`;
      });
      $(".category").html(html);
    } else {
      alert(res.data.message);
    }
  });

  const params = {
    current: 1,
    pagesize: 12,
    category: "",
    filter: "",
    saleType: 10,
    sortType: "id",
    sortMethod: "ASC",
    search: "",
  };

  $(".category").on("click", "li", function () {
    $(".category li").removeClass("active");
    $(this).addClass("active");
    params.category = $(this).text();
    if (params.category === "全部") params.category = "";
    params.current = 1;
    getList();
  });

  $(".next").on("click", function () {
    if (params.current < totalPage) {
      params.current++;
      $(".prev").removeClass("disable");
      getList();
    }
  });
  $(".prev").on("click", function () {
    if (params.current > 1) {
      params.current--;
      if (params.current === 1) {
        $(".prev").addClass("disable");
      }
      getList();
    }
  });

  $(document).on("click", ".saleBox li", function () {
    $(".saleBox li").removeClass("active");
    $(this).addClass("active");
    params.saleType = $(this).data("type") || $(this).data("method");
    params.current = 1;
    getList();
  });

  $(document).on("click", ".hotBox li", function () {
    $(".hotBox li").removeClass("active");
    $(this).addClass("active");
    params.filter = $(this).data("type") || $(this).data("method");
    params.current = 1;
    getList();
  });

  $(document).on("click", ".sortBox li", function () {
    $(".sortBox li").removeClass("active");
    $(this).addClass("active");
    params.sortType = $(this).data("type") || $(this).data("method");
    params.current = 1;
    getList();
  });

  $(".search").on("input", function () {
    params.search = $(this).val();
    getList();
  });

  $(".go").on("click", function () {
    let pageNum = parseInt($(".jump").val(), 10);
    if (!isNaN(pageNum) && pageNum > 0) {
      params.current = pageNum;
      getList();
    } else {
      alert("请输入有效的页码");
    }
  });

  $(".pagesize").on("change", function () {
    params.pagesize = parseInt($(this).val(), 10);
    params.current = 1;
    getList();
  });

  function getList() {
    axios
      .get("http://localhost:9000/goods/list", { params: params })
      .then(function (res) {
        if (res.data.code === 1) {
          let html = res.data.list
            .map(function (product) {
              return `
                      <li data-id="${product.goods_id}">
                          <div class="show">
                              <img src="${product.img_big_logo}">
                              ${
                                product.is_hot
                                  ? '<span class="hot">热销</span>'
                                  : ""
                              }
                              ${product.is_sale ? "<span>折扣</span>" : ""}
                          </div>
                          <div class="info">
                              <p class="title">${product.title}</p>
                              <p class="price">
                                  <span class="curr">¥${
                                    product.current_price
                                  }</span>
                                  <span class="old">¥${product.price}</span>
                              </p>
                          </div>
                      </li>
                  `;
            })
            .join("");
          $(".list.container").html(html);
          $(".total").text(`${params.current}/${res.data.total}`);
          totalPage = res.data.total;
        } else {
          alert(res.data.message);
        }
      });
  }
  getList();
  $(".list.container").on("click", "li", function () {
    let id = $(this).data("id");
    sessionStorage.setItem("productId", id);
    window.location.href = "./detail.html";
  });
});
